﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MainConsoleService.OrderDownService;
using MainConsoleService.Common;
using MainConsoleService.PrePackaged;
using MainConsoleService.PromotionOrderDo;
using System.Collections;
using MainConsoleService.DownOrderToERP;

namespace DownOrderApplication
{
    public partial class FrmServiceTookKit : Form
    {

        public FrmServiceTookKit()
        {
            InitializeComponent();
        }

        private void FrmDownOrder_Load(object sender, EventArgs e)
        {
            CocoNut.BLL.OrderDownService.T_ERP_ShopInfo bll = new CocoNut.BLL.OrderDownService.T_ERP_ShopInfo();
            List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo> listModel = new List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo>();
            listModel = bll.GetModelList(" IsAutoDown=1 ");            
            cbb_shop.DataSource = listModel;
            cbb_shop.DisplayMember = "ShopName";
            cbb_shop.ValueMember = "ShopNumber";            
            txtbgtime.Text = System.DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
            txtedtime.Text = System.DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";   
            DownLoadOrder dow=new  DownLoadOrder();
            dow.AsynBillDataToERP("20", "壳壳果建行旗舰店", "admin", "admin", false);
        }

        //根据订单时间下载
        private void button1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {

                MessageBox.Show("店铺名称必须选择！");
                this.Cursor = Cursors.Arrow;
                return;
            }

            string errmsg = "";
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            string bgtime = txtbgtime.Text.Trim();
            string edtime = txtedtime.Text.Trim();
            errmsg = DownOrderHand.GetOrderByTime(bgtime, edtime, shopId);
            if (errmsg.Length==0)
            {
                MessageBox.Show("订单下载成功！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            else
            {
                MessageBox.Show(errmsg);
                this.Cursor = Cursors.Arrow;
                return;
            }

        }

        //根据订单编号
        private void button2_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string msg = CheckData();
            if (msg.Length > 0)
            {
                MessageBox.Show(msg);
                this.Cursor = Cursors.Arrow;
                return;
            }
            string errmsg = "";
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            string orderId = txtOderId.Text.Trim();
            errmsg = DownOrderHand.GetOrderById(orderId, shopId);
            if (errmsg.Length==0)
            {
                MessageBox.Show("订单下载成功！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            else
            {
                MessageBox.Show(errmsg);
                this.Cursor = Cursors.Arrow;
                return;
            }
        }

        public string CheckData()
        {
            string msg = "";

            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {
                msg += "店铺名称必须选择！\r\n";
            }

            if (txtOderId.Text.Trim().Length == 0)
            {
                msg += "订单编号必须填写！\r\n";
            }
            if (txtBuynick.Text.Trim().Length == 0)
            {
                msg += "会员名称必须填写！\r\n";
            }


            return msg;
        }

        private void btnSendGoods_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {

                MessageBox.Show("店铺名称必须选择！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            bool isOk = false;
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            try
            {
                CocoNut.BLL.OrderDownService.T_ERP_ShopInfo bll = new CocoNut.BLL.OrderDownService.T_ERP_ShopInfo();
                List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo> listModel = new List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo>();
                listModel = bll.GetModelList(string.Format(" IsAutoDown=1 and ShopNumber={0} ", shopId));
                foreach (CocoNut.Model.OrderDownService.T_ERP_ShopInfo model in listModel)
                {
                    switch (model.ShopNumber)
                    {
                        case 1:
                            TopTmallOrderDown tmall = new TopTmallOrderDown(model);
                            tmall.SendLogisticsInfo(model);
                            break;
                        case 2:
                            PaiPaiOrderDown pp = new PaiPaiOrderDown(model);
                            pp.SendLogisticsInfo(model);
                            
                            break;
                        case 3:
                            JDOrderDown jd = new JDOrderDown(model);
                            jd.SendLogisticsInfo();
                            break;
                        case 9:
                            TopTmallOrderDown tmall9 = new TopTmallOrderDown(model);
                            tmall9.SendLogisticsInfo(model);
                            break;
                        case 10:
                            ICBCOrderDown icbc = new ICBCOrderDown(model);
                            icbc.SendLogisticsInfo(model);
                            break;
                        case 13:
                            YhdOrderDown yhd = new YhdOrderDown(model);
                            yhd.SendLogisticsInfo();
                            break;
                        case 14:
                            DangDangOrderDown dang = new DangDangOrderDown(model);
                            dang.SendLogisticsInfo(model);
                            break;
                        case 15:
                            VipShopOrderDown vip = new VipShopOrderDown(model);
                            vip.SendLogisticsInfo(model);
                            break;
                        case 17:
                            SuningOrderDown so = new SuningOrderDown(model);
                            so.SuningCustomOrderDeliveryAdd();
                            break;
                        case 30:
                            WeChatOrderDown we = new WeChatOrderDown(model);
                            we.SendLogisticsInfo(model);
                            break;
                    }
                    isOk = true;
                }
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                isOk = false;
                WriteSysLog.Log("ServerLog", string.Format("订单发货服务运作异常，异常信息{0},异常时间{1}\r\n", ex.Message, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            finally
            {
                if (isOk)
                    MessageBox.Show("发货成功！");
                else
                    MessageBox.Show("发货失败！");

            }
        }


        
        private void btnYdb_Click(object sender, EventArgs e)
        {
            string strwhere = "";
            this.Cursor = Cursors.WaitCursor;
            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {
                MessageBox.Show("店铺名称必须选择！");
                this.Cursor = Cursors.Default;
                return;
            }
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            CocoNut.BLL.OrderDownService.t_ddzb ddzbBll = new CocoNut.BLL.OrderDownService.t_ddzb();
            strwhere = string.Format(" hb_zt = '否' and dpid='{0}' and ks=1 and wlddy=0 and wldsd=0 ", shopId);
            List<CocoNut.Model.OrderDownService.t_ddzb> ddzbList = ddzbBll.GetModelList(strwhere);
            try
            {
                if (ddzbList != null && ddzbList.Count > 0)
                {
                    foreach (CocoNut.Model.OrderDownService.t_ddzb model in ddzbList)
                    {
                        if (!model.ddhb) //不是合并订单预打包
                        {
                            OrderPrePackaged.DoPrepackagedForPrint(model.tid, model.guid, model.dpid);
                        }
                    }
                }
                MessageBox.Show("预打包成功！");
            }
            catch
            {
                MessageBox.Show("预打包失败！");
            }
            this.Cursor = Cursors.Default;

        }

        private void btnpayment_Click(object sender, EventArgs e)
        {
            //ServiceTest st = new ServiceTest();
            //st.UpdatePayment(txtbgtime.Text.Trim(), txtedtime.Text.Trim(),cbb_shop.SelectedValue.ToString());
        }

       

        private void btnVipDirect_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {

                MessageBox.Show("店铺名称必须选择！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            try
            {
                CocoNut.Model.OrderDownService.T_ERP_ShopInfo model = null;
                CocoNut.BLL.OrderDownService.T_ERP_ShopInfo bll = new CocoNut.BLL.OrderDownService.T_ERP_ShopInfo();
                List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo> listModel = new List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo>();
                listModel = bll.GetModelList(string.Format(" IsAutoDown=1 and ShopNumber={0} ", shopId));
                model = listModel[0];
                switch (shopId)
                {
                    case 1:
                        TopTmallOrderDown tmall = new TopTmallOrderDown(model);
                        break;
                    case 2:
                        PaiPaiOrderDown pp = new PaiPaiOrderDown(model);
                        break;
                    case 3:
                        JDOrderDown jd = new JDOrderDown(model);
                        break;
                    case 9:
                        TopTmallOrderDown tmall9 = new TopTmallOrderDown(model);
                        break;
                    case 10:
                        ICBCOrderDown icbc = new ICBCOrderDown(model);
                        break;
                    case 13:
                        YhdOrderDown yhd = new YhdOrderDown(model);
                        yhd.PrintSendLogisticsInfo();
                        break;
                    case 14:
                        DangDangOrderDown dang = new DangDangOrderDown(model);
                        break;
                    case 15:
                        VipShopOrderDown vip = new VipShopOrderDown(model);
                        vip.PrintToSendLogisticsInfo(model);
                        break;
                    case 17:
                        SuningOrderDown so = new SuningOrderDown(model);
                        break;
                    case 30:
                        WeChatOrderDown we = new WeChatOrderDown(model);
                        break;
                } 
                MessageBox.Show("发货成功！");
            }
            catch
            {
                MessageBox.Show("发货异常！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (cbb_shop.SelectedValue.ToString() == "请选择")
            {

                MessageBox.Show("店铺名称必须选择！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            try
            {
                CocoNut.BLL.OrderDownService.T_ERP_ShopInfo bll = new CocoNut.BLL.OrderDownService.T_ERP_ShopInfo();
                List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo> listModel = new List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo>();
                listModel = bll.GetModelList(string.Format(" IsAutoDown=1 and ShopNumber={0} ", shopId));
                VipShopOrderDown vip = new VipShopOrderDown(listModel[0]);
                vip.SendLogisticsInfoByTid(listModel[0], txtOderId.Text.Trim());
                MessageBox.Show("发货成功！");
            }
            catch
            {
                MessageBox.Show("发货异常！");
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Hashtable hast = new Hashtable();
            decimal payment = 0.0M;
            CocoNut.BLL.OrderDownService.T_ERP_ShopInfo bll = new CocoNut.BLL.OrderDownService.T_ERP_ShopInfo();
            List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo> listModel = new List<CocoNut.Model.OrderDownService.T_ERP_ShopInfo>();
            listModel = bll.GetModelList(string.Format(" IsAutoDown=1 and ShopNumber={0} ", cbb_shop.SelectedValue));
            SuningOrderDown sunning = new SuningOrderDown(listModel[0]);

            CocoNut.BLL.OrderDownService.t_ddzb ddzb = new CocoNut.BLL.OrderDownService.t_ddzb();
            List<CocoNut.Model.OrderDownService.t_ddzb> list = ddzb.GetModelList(string.Format(" pay_time >='{0}' and pay_time<='{1}' and dpid={2} ", txtbgtime.Text.Trim() , txtedtime.Text.Trim(),
                cbb_shop.SelectedValue));
            foreach (CocoNut.Model.OrderDownService.t_ddzb model in list)
            {
                payment = sunning.GetSuningOrderByIdForPayment(model.ytid).Value;
                if (payment != model.payment && payment != 0.0M)
                {
                    ddzb.UpdatePayment(model.id, payment, ref hast);
                    WriteSysLog.Log("PaymentLog", string.Format("tid:{0},payment:{1}\r\n", model.tid, payment));
                }
            }
            try
            {
                DbHelperSQL.ExecuteSqlTran(hast);
                MessageBox.Show("更新Payment成功！");
            }
            catch
            {
                MessageBox.Show("更新Payment异常！");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int? wlgsId=0;
            string wlgs = "";
            wlgs = DownOrderCommon.GetLogistInfo(ref wlgsId, "宁国市", "安徽省", "南山办事处");
            wlgs = DownOrderCommon.GetLogistInfo(ref wlgsId, "上海市", "上海市", "黄埔区");
            wlgs = DownOrderCommon.GetLogistInfo(ref wlgsId, "北京市", "北京市", "西城区");
            wlgs = DownOrderCommon.GetLogistInfo(ref wlgsId, "天津市", "天津市", "和平区");
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string msg = CheckData();
            if (msg.Length > 0)
            {
                MessageBox.Show(msg);
                this.Cursor = Cursors.Arrow;
                return;
            }
            string errmsg = "";
            int shopId = int.Parse(cbb_shop.SelectedValue.ToString());
            string nickId = txtBuynick.Text.Trim();
            errmsg = DownOrderHand.GetOrderByBuyerNick(nickId, shopId);
            if (errmsg.Length == 0)
            {
                MessageBox.Show("订单下载成功！");
                this.Cursor = Cursors.Arrow;
                return;
            }
            else
            {
                MessageBox.Show(errmsg);
                this.Cursor = Cursors.Arrow;
                return;
            }
        }
    }
}
